import { Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

/**
 * 跨平台存储工具
 * Web端使用localStorage，移动端使用AsyncStorage
 */
export class Storage {
  
  /**
   * 获取存储的值
   */
  static async getItem(key: string): Promise<string | null> {
    try {
      if (Platform.OS === 'web') {
        return localStorage.getItem(key);
      } else {
        return await AsyncStorage.getItem(key);
      }
    } catch (error) {
      console.error('获取存储值失败:', error);
      return null;
    }
  }

  /**
   * 设置存储的值
   */
  static async setItem(key: string, value: string): Promise<void> {
    try {
      if (Platform.OS === 'web') {
        localStorage.setItem(key, value);
      } else {
        await AsyncStorage.setItem(key, value);
      }
    } catch (error) {
      console.error('设置存储值失败:', error);
      throw error;
    }
  }

  /**
   * 删除存储的值
   */
  static async removeItem(key: string): Promise<void> {
    try {
      if (Platform.OS === 'web') {
        localStorage.removeItem(key);
      } else {
        await AsyncStorage.removeItem(key);
      }
    } catch (error) {
      console.error('刪除存儲值失敗:', error);
      throw error;
    }
  }

  /**
   * 清空所有存储
   */
  static async clear(): Promise<void> {
    try {
      if (Platform.OS === 'web') {
        localStorage.clear();
      } else {
        await AsyncStorage.clear();
      }
    } catch (error) {
      console.error('清空存儲失敗:', error);
      throw error;
    }
  }

  /**
   * 获取所有存储的键
   */
  static async getAllKeys(): Promise<string[]> {
    try {
      if (Platform.OS === 'web') {
        return Object.keys(localStorage);
      } else {
        return await AsyncStorage.getAllKeys();
      }
    } catch (error) {
      console.error('獲取所有鍵失敗:', error);
      return [];
    }
  }

  /**
   * 批量获取多个值
   */
  static async multiGet(keys: string[]): Promise<[string, string | null][]> {
    try {
      if (Platform.OS === 'web') {
        return keys.map(key => [key, localStorage.getItem(key)]);
      } else {
        return await AsyncStorage.multiGet(keys);
      }
    } catch (error) {
      console.error('批量获取值失败:', error);
      return keys.map(key => [key, null]);
    }
  }

  /**
   * 批量设置多个值
   */
  static async multiSet(keyValuePairs: [string, string][]): Promise<void> {
    try {
      if (Platform.OS === 'web') {
        keyValuePairs.forEach(([key, value]) => {
          localStorage.setItem(key, value);
        });
      } else {
        await AsyncStorage.multiSet(keyValuePairs);
      }
    } catch (error) {
      console.error('批量设置值失败:', error);
      throw error;
    }
  }

  /**
   * 批量删除多个值
   */
  static async multiRemove(keys: string[]): Promise<void> {
    try {
      if (Platform.OS === 'web') {
        keys.forEach(key => {
          localStorage.removeItem(key);
        });
      } else {
        await AsyncStorage.multiRemove(keys);
      }
    } catch (error) {
      console.error('批量删除值失败:', error);
      throw error;
    }
  }
}
